﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Lon.Server
{
    public partial class ConfigEditForm : Form
    {
        readonly string CONFIG_FILE = "SysConfig.ini";
        bool dirty = false;

        public ConfigEditForm()
        {
            InitializeComponent();
        }

        string GetConfigFile()
        {
            return Path.Combine(Application.StartupPath, CONFIG_FILE);
        }

        protected override void OnLoad(EventArgs e)
        {
            LoadConfig();
            base.OnLoad(e);
        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            if (dirty)
            {
                if (MessageBox.Show("配置文件已修改，是否保存?   ", "保存", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    SaveConfig();
                }
            }

            base.OnFormClosing(e);
        }

        void LoadConfig()
        {
            string fileName = GetConfigFile();
            statusLabel1.Text = fileName;
            textBox1.Text = File.ReadAllText(fileName);
            textBox1.SelectionStart = 0;
            textBox1.SelectionLength = 0;
            dirty = false;
        }

        void SaveConfig()
        {
            if (!dirty) return;
            string fileName = GetConfigFile();
            File.WriteAllText(fileName, textBox1.Text);
            dirty = false;
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            dirty = true;
        }

        private void mnuExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void mnuSave_Click(object sender, EventArgs e)
        {
            SaveConfig();
        }


    }
}
